package com.good.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import com.good.entity.PdfPage;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfUtile {
	public final static String FILE_DIR = "D:/";

	private PdfUtile() {

	}

	/**
	 * 创建一个pdf文件
	 * 
	 * @param pdfName
	 *            文件名字
	 * @param content
	 *            文件内容
	 * @param userPassword
	 * 
	 * @param ownerPassword
	 *            打开密码
	 */
	public final static void create(String pdfName, String content,
			String userPassword, String ownerPassword) {
		// Step 1—Create a Document
		Document document = new Document();
		try {
			// Step 2—Get a PdfWriter instance.
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream(FILE_DIR + pdfName + ".pdf"));
			writer.setEncryption(userPassword.getBytes(),
					ownerPassword.getBytes(), PdfWriter.ALLOW_SCREENREADERS,
					PdfWriter.STANDARD_ENCRYPTION_128);
			// Step 3—Open the Document.
			document.open();
			// Step 4—Add content.
			document.add(new Paragraph(content));
			// Step 5—Close the Document.
			document.close();
		} catch (FileNotFoundException | DocumentException e) {
			document.close();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 创建pdf
	 * 
	 * @param size
	 *            文件页面大小(PageSize.B5.rotate())
	 * @param color
	 *            背景颜色(BaseColor.WHITE)
	 * @param pdfName
	 *            文件名字
	 * @param title
	 *            标题
	 * @param author
	 *            作者
	 * @param subject
	 *            主题
	 * @param keyWords
	 *            关键字
	 * @param creator
	 *            由谁创建
	 */
	public final static void create(String pdfName, Rectangle size,
			BaseColor color, String userPassword, String ownerPassword,
			String title, String author, String subject, String keyWords,
			String creator) {
		// 页面大小
		Rectangle rect = new Rectangle(size);
		// 页面背景色
		rect.setBackgroundColor(color);
		Document doc = new Document(rect);
		PdfWriter writer;
		try {
			writer = PdfWriter.getInstance(doc, new FileOutputStream(FILE_DIR
					+ pdfName + ".pdf"));
			// PDF版本(默认1.4)
			writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
			writer.setEncryption(userPassword.getBytes(),
					ownerPassword.getBytes(), PdfWriter.ALLOW_SCREENREADERS,
					PdfWriter.STANDARD_ENCRYPTION_128);
			// 文档属性
			doc.addTitle(title);
			doc.addAuthor(author);
			doc.addSubject(subject);
			doc.addKeywords(keyWords);
			doc.addCreator(creator);
			// 页边空白
			doc.setMargins(100, 200, 30, 40);
			doc.open();
			doc.add(new Paragraph("Hello World"));
			doc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			doc.close();
		}
	}

	public final static boolean addPages(String pdfName, String path,
			List<PdfPage> pdfPage) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(path + pdfName + ".pdf");
		} catch (FileNotFoundException e) {
			return false;
		}
		Document document = new Document();
		PdfWriter writer = null;
		try {
			writer = PdfWriter.getInstance(document, out);
		} catch (DocumentException e) {
			return false;
		}
		document.open();
		try {
			for (int i = 0; i < pdfPage.size(); i++) {
				if (i == 0) {
					document.add(new Paragraph(pdfPage.get(i).getContent()));
				}
				writer.setPageEmpty(false);
				document.newPage();
				document.add(new Paragraph(pdfPage.get(i).getContent()));
			}
		} catch (DocumentException e) {
			return false;
		}
		document.close();
		return true;
	}

	public static void main(String[] args) throws Exception {
		/* PdfUtile.create("first","good","11111","mypassword"); */
		/*
		 * PdfUtile.create(PageSize.B5.rotate(), BaseColor.WHITE, "two", "标题",
		 * "作者名字", "主题", "关键字", "创建人");
		 */
		// addPages();
	}
}
